#pragma once

#define ESP8266

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <set>
#include "define.h"

class MqttClient {
public:
    MqttClient();
    ~MqttClient();

    void setup();
    void loop();

    void scan();
    void connect();
    void callback(char* topic, uint8_t* payload, unsigned int length);
public:
    void pinOn(const char* data, unsigned int length);
    void pinOff(const char* data, unsigned int length);
    void pinState(const char* data, unsigned int length);
private:
    void syncPublish(bool show = false);
    void pinOnOff(portcfg* cfg, bool state);
private:
    WiFiClient espClient;                       // wifi客户端
    PubSubClient* client;                       // 发布客户端
    uint32_t updtick;                           // 更新tick

    int16_t choose;                             // 配置选择
    portcfgMap pinmap;                          // 端口列表
    int16_t failCnt;                            // 失败次数
    String smac;                                // mac地址
    String sip;                                 // ip地址
    String c2sTopic;                            // 客户端到服务器的topic
    String s2cTopic;                            // 服务器到客户端的topic
    StringSumHelper pubString;                  // 发布的数据
    bool syncState;                             // 同步状态
    bool subSuccess;                            // 订阅成功

    static const char* c2sHTopic;               // 客户端到服务器topic头
    static uint32_t pubTick;                    // 发布的间隔
    static const char* s2cHTopic;               // 服务器到客户端的topic头
};